import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'package:xuance/RoutePath.dart';
import 'package:xuance/bean/PublicKeyBean.dart';
import 'package:xuance/bean/exp.dart';
import 'package:xuance/network/api/CommonApi.dart';
import 'package:xuance/util/cache_util.dart';
import 'package:xuance/util/toast_util.dart';

class SplashLogic extends GetxController {
  @override
  void onInit() {
    super.onInit();
    init();
  }

  Future<void> init() async {
    try {
      PublicKeyBean? publicKey = await CommonApi.getPubKeyCached();
      if (publicKey == null) {
        showToast("连接服务器失败");
      } else {
        if (await CacheUtil.getUser() != null) {
          Get.offAllNamed(RoutePath.home);
        } else {
          Get.offAllNamed(RoutePath.login);
        }
      }
    } catch (e) {
      if (e is ApiException) {
        showToast(e.message);
      } else if (e is DioException) {
        print(e.message);
      } else {
        showToast("$e");
      }
    }
  }
}
